//
//  VMCCartModuleViewController.m
//  vmcshop
//
//  Created by bigknife on 14-10-9.
//  Copyright (c) 2014年 idongler. All rights reserved.
//

#import "VMCCartModuleViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "UMSocial.h"
#import "NTViewController.h"
#import "NTButton.h"

@interface VMCCartModuleViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic,strong) UIView * topView;
@property (nonatomic,strong) UIView *iv;
@property (nonatomic,strong) AnimationView *avi;
@property (weak, nonatomic) IBOutlet UIView *footView;
@property (nonatomic,strong) UIView * statusBar;
@end

@implementation VMCCartModuleViewController
NSMutableURLRequest *urlRequest;
- (void)viewDidLoad
{
	[super viewDidLoad];
	[self showView];
}


#pragma mark <UIWebViewDelegate>
- (void)webViewDidStartLoad:(UIWebView *)webView{
	NSLog(@"执行了这句话没");
	[MBProgressHUD showHUDAddedTo:self.webView animated:YES];
	
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	[MBProgressHUD hideHUDForView:self.webView animated:YES];
	[_statusBar setBackgroundColor:[UIColor colorWithString:@"#A52E8D"]];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	[MBProgressHUD hideHUDForView:self.webView animated:YES];
	TOAST(error.localizedDescription);
}

//获取webview
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
	//请求的url路径
	NSString * urlStr = request.URL.absoluteString;
	
	//1.查找－在url中的范围,判断当前页面是否需要隐藏tabbar，详情和三级页面需要隐藏
	NSRange range1 = [urlStr rangeOfString:@"detail"];
	NSRange range2 = [urlStr rangeOfString:@"item"];
	if (range1.length || range2.length) {
		
		self.tabBarController.tabBar.hidden = YES;
		CGRect frame = _webView.frame;
		frame.size.height = MyHeight-20;
		_webView.frame = frame;
	}
	else{
		self.tabBarController.tabBar.hidden = NO;
		CGRect frame = _webView.frame;
		frame.size.height = MyHeight-69;
		_webView.frame = frame;
	}
	
	NSString * requestUrl = request.mainDocumentURL.relativeString;
	
	if ([requestUrl isEqualToString:HTML_URL]) {
		//遍历得到的cookie。
		NSHTTPCookieStorage *myCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];
		for (NSHTTPCookie *cookie in [myCookie cookies]) {
			NSLog(@"%@", cookie);
			//为了实现自动登录，需要将cookie保存下来，以便自动登录，只需要加上一行代码。
			[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; // 保存
		}
		VMCIndexPageViewController * index = [[VMCIndexPageViewController alloc] init];
		[self.navigationController pushViewController:index animated:NO];
	}
	
	//2.判断当前是哪个界面，tabbar的button对应相同的按钮
	if ([urlStr rangeOfString:@"index"].length) {
		[self chooseBtnClick:0];
		return NO;
	}
	if ([urlStr rangeOfString:@"school"].length ) {
		[self chooseBtnClick:1];
		return NO;
	}
	if ([urlStr rangeOfString:@"list"].length) {
		[self chooseBtnClick:2];
		return NO;
	}
	if ([urlStr rangeOfString:CART_URL].length) {
		[self chooseBtnClick:3];
		return YES;
	}
	if ([urlStr rangeOfString:@"my"].length) {
		[self chooseBtnClick:4];
		return NO;
	}
	
	//3.首先创建JSContext 对象（此处通过当前webView的键获取到jscontext）
	JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
	NSString *alertJS=@"window.jsToNative.showNavigationBar"; //准备执行的js代码
	[context evaluateScript:alertJS];//通过oc方法调用js的alert
	
	return YES;
}
- (IBAction)lifeBtnClick:(UIButton *)sender {
	[self chooseBtnClick:sender.tag];
}
- (void) showView{
//	_topView					= [[UIView alloc] init];
//	_topView.frame				= CGRectMake(0, 0, MyWidth, 28);
//	_topView.backgroundColor	= MainColor;
//	[self.view					addSubview:_topView];
	
	_statusBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MyWidth, 20)];
	[_statusBar setBackgroundColor:[UIColor whiteColor]];
	[self.view addSubview:_statusBar];
	self.navigationController.navigationBar.hidden = YES;
	_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,20,MyWidth,MyHeight-69)];
	urlRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTML_URL,CART_URL]]];
	[self.view addSubview:_webView];
	_webView.delegate=self;
	[urlRequest setHTTPShouldHandleCookies:YES];
	[_webView loadRequest:urlRequest];
	
	for(id subview in _webView.subviews)
	{
		if ([[subview class] isSubclassOfClass:[UIScrollView class]]) {
			((UIScrollView *)subview).bounces = NO;
		}
		
	}
	_webView.scrollView.bounces=NO;
	
}
#pragma mark - 登录界面隐藏底部导航按钮
/*
 * 登录界面隐藏底部导航按钮
 */
- (void) loginUrl{
	_webView.frame = CGRectMake(0,0,MyWidth,MyHeight);
	//self.footView.hidden = YES;
}

#pragma mark tabbar根据页面显示相应的按钮
- (void)chooseBtnClick:(NSInteger)tag{
	NTViewController * tbVC = (NTViewController *)self.tabBarController;
	
	if(tbVC.selectedIndex != tag){ //wsq®
		tbVC.selectedIndex = tag; //切换不同控制器的界面
		tbVC.previousBtn.selected = ! tbVC.previousBtn.selected;
		
		for (id  view in tbVC.tabBarView.subviews) {
			if ([view isKindOfClass:[NTButton class]]) {
				NTButton * btn = (NTButton *)view;
    if (btn.tag == tag) {
		tbVC.previousBtn = btn;
	}
			}
		}
		tbVC.previousBtn.selected = YES;
	}
}

- (void) jsClick{

}
- (void)dealloc{
	_webView.delegate = nil;
}
#pragma mark - 分享（微信分享）
/*
 * 分享（微信分享）
 */
- (void) shareWeiXin:(NSString *)shareStr{
	//注意：分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
	NSArray * shareArr = [shareStr componentsSeparatedByString:@","];
	NSString *titleStr = [shareArr[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSString *imageStr = shareArr[2];
	NSString *shareUrl = shareArr[3];
	//网络图片转换成image
	UIImage * result;
	NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageStr]];
	result = [UIImage imageWithData:data];
	//	//微信好友
	[UMSocialData defaultData].extConfig.wechatSessionData.title				= titleStr;
	[UMSocialData defaultData].extConfig.wechatSessionData.shareImage			= result;
	[UMSocialData defaultData].extConfig.wechatSessionData.url					= shareUrl;
	
	//	//微信朋友圈
	[UMSocialData defaultData].extConfig.wechatTimelineData.title				= titleStr;
	[UMSocialData defaultData].extConfig.wechatTimelineData.shareImage			= result;
	[UMSocialData defaultData].extConfig.wechatTimelineData.url					= shareUrl;
	[UMSocialSnsService presentSnsIconSheetView:self
										 appKey:nil
									  shareText:titleStr
									 shareImage:result
								shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,nil]
									   delegate:self];
}

//设置状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
	return UIStatusBarStyleLightContent;
}

#pragma mark - 试图即将出现的时候，让网页刷新
- (void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
	[self showView];
}

@end
